/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Manager;

import Global.Global;
import Operations.OperationManager;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

 /**
 *
 * @author littlechild
 */
public class Mg implements Manager{
    ArrayList<BufferedImage> images=new ArrayList<BufferedImage>();
    ArrayList<String> recentFiles=new ArrayList<String>();
    OperationManager opManager=new OperationManager();
    int fileNums=5;
    //int imageNums=20;
    int currentImage=-1;   //the index of the array
    boolean ispreview=false;   //mode variable
    public BufferedImage operate(int op, String[] paras) {
        return operate(op, paras,false);
    }
    public BufferedImage operate(int op, String[] paras, boolean isp) {
        //try{
            ispreview=isp;
            if(op>=Global.fileBegin && op<Global.fileEnd) {
                //this operation is file operation
                return fileOperate(op,paras);
            }else if(op>=Global.editBegin && op<Global.editEnd) { 
                //op is a editoperation
                return editOperate(op,paras);
            }else if(op>=Global.opBegin && op<Global.opEnd) {
                //real opFunction
                return funcOperate(op,paras);
            } else if (op==Global.opNone) {
                //none operation, just to facilitate GUI in determining which image is to be shown
                if(currentImage>=0 && currentImage<images.size())
                    return images.get(currentImage);
                else
                    return null;
            }
        //}catch(NullPointerException e) {
        //    System.out.println("请先打开文件");
        //}
        return null;
    }
    BufferedImage fileOperate(int op,String[] paras) {
        if(op==Global.fileopen){
            try {
                if (recentFiles.size() == fileNums) {
                    recentFiles.remove(0);
                }
                recentFiles.add(paras[0]);
                BufferedImage incoming = javax.imageio.ImageIO.read(new File(paras[0]));
                images.add(incoming);
                recentFiles.add(paras[0]);
                currentImage=images.size()-1;
                return images.get(images.size()-1);
            } catch (IOException ex) {
                Logger.getLogger(Mg.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(op==Global.fileclose) {
            images.remove(images.size()-1);
        }else if(op==Global.filesave) {
            try {
                ImageIO.write(images.get(images.size()-1), "bmp", new File(recentFiles.get(recentFiles.size() - 1)));
            } catch (IOException ex) {
                Logger.getLogger(Mg.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(op==Global.filesaveas) {
            try {
                ImageIO.write(images.get(images.size()-1), "bmp", new File(paras[0]));
            } catch (IOException ex) {
                Logger.getLogger(Mg.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    private BufferedImage editOperate(int op, String[] paras) {
        if(op==Global.editBack) {
            if(currentImage>=1) {
                currentImage--;
                images.add(images.get(currentImage));
                return images.get(currentImage);
            }
        }else if(op==Global.editFor) {
            if(images.size()-currentImage>=2) {
                currentImage++;
                images.add(images.get(currentImage));
                return images.get(currentImage);
            }
        }
        return null;
    }
    private BufferedImage funcOperate(int op, String[] paras) {
        BufferedImage returnValue=null;
        double[] parasDouble=null;
        if(op==Global.opMinuswith) {
            returnValue=new Operations.Algebra.OpCptWith().operate(images.get(currentImage), paras[0],false);
        }else if(op==Global.opAddwith) {
            returnValue=new Operations.Algebra.OpCptWith().operate(images.get(currentImage), paras[0],true);
        }else {   
            if(paras!=null) {
                parasDouble=new double[paras.length+1];
                for(int i=0;i!=paras.length;++i) {
                    parasDouble[i]=Double.valueOf(paras[i]).doubleValue();
                }
                parasDouble[paras.length]=op;
            }else {
                parasDouble=new double[]{op};
            }
            returnValue=opManager.operate(op,images.get(currentImage),parasDouble);
        }
        if(ispreview==false) {
            images.add(returnValue);
            currentImage=images.size()-1;
        }
        return returnValue;
    }


}

